<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>填写日志</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		
		<link href="../../../css/mui.min.css" rel="stylesheet"/>
		<link href="../../../css/global.css" rel="stylesheet"/>
		<link href="../../../css/style.css" rel="stylesheet"/>
		<link href="../../../css/task.css" rel="stylesheet"/>

		<style type="text/css">
			#div {
				width: 0px;
				height: 0px;
				background: red;
				position: fixed;
				top: 65%;
				left: 50%;
			}
					
			/*移除底部或顶部三角,需要在删除此代码*/
			.mui-popover .mui-popover-arrow:after {
				width: 0px;
			}

			.details_con{
				margin: 0;
			}
			/* input.d_ipt {
				width: auto;
			} */
		</style>
	</head>

	<body>
		<div id="div"></div>
		<div id="popover" class="mui-popover shadow popover">
		 <p class="title" id="title">已关联多个服务通知，请确认工时是否拆分？</p >
		 <div class="btn_box">
		  <span class="fl" onclick="saveLog('0')">否</span>
		  <span class="fr red" onclick="saveLog('1')">是</span>
		 </div>
		</div>
		
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">填写日志</h1>
		</header>
		<div class="mui-content">
			<div class="details_con" id="form_info">
				<div class="page_cen shadow title_num" id="dianji">服务通知编号：<span id="serviceNo" style="color: #ea1e1a;"></span></div>
				<div hidden>
					<input type="hidden" id="fseId" name="fseId"/>
					<input type="hidden" id="serviceNoticeIds" name="serviceNoticeIds"/>
					<input type="hidden" id="serviceNos" name="serviceNos"/>
					<input type="hidden" id="planIds" name="planIds"/>
					<input type="hidden" id="fseName" name="fseName"/>
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="hdyqxlh">
						<em>核对仪器序列号</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="clum">
					<span class="name">接受任务时间</span>
					<input type="text" class="d_ipt" id="acceptTime" name="acceptTime" disabled="disabled"/>
				</div>
				<div class="clum">
					<span class="name">出发时间<em class="t_red">*</em></span>
					<input type="text" class="d_ipt" id="departureTime" name="departureTime" required="required" readonly="readonly" placeholder="请选择出发时间" />
				</div>
				<div class="clum">
					<span class="name">到达现场时间<em class="t_red">*</em></span>
					<input type="text" class="d_ipt" id="arriveTime" name="arriveTime" required="required" readonly="readonly" placeholder="请选择到达现场时间" />
				</div>
				<div class="clum">
					<span class="name">工作开始时间<em class="t_red">*</em></span>
					<input type="text" class="d_ipt" id="startTime" name="startTime" required="required" readonly="readonly" placeholder="请选择开始时间" />
				</div>
				<div class="clum">
					<span class="name">工作结束时间<em class="t_red">*</em></span>
					<input type="text" class="d_ipt" id="endTime" name="endTime" required="required" readonly="readonly" placeholder="请选择结束时间" />
				</div>
				<div class="clum">
					<span class="name">工作小时数<em class="t_red">*</em></span>
					<input type="number" class="d_ipt" id="workHour" name="workHour" required="required" maxlength="4" placeholder="请输入工作小时数" />
				</div>
				<div class="clum">
					<span class="name">旅途小时数<em class="t_red">*</em></span>
					<input type="number" class="d_ipt" id="journeyHour" name="journeyHour" required="required" maxlength="4" placeholder="请输入旅途小时数" />
				</div>
				<div class="clum nobor autoHeight">
					<span class="name">工作内容<em class="t_red">*</em></span>
					<div class="clear"></div>
					<textarea class="textarea" id="workContent" name="workContent" required="required" maxlength="500" placeholder="请输入工作内容"></textarea>
					
				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="glfwtz">
						<em>关联的服务通知</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
			</div>
			<div class="btn_box">
				<button class="fl" id="cancelBtn" type="button">取消</button>
				<button class="fr red" id="submitLogBtn" type="button">保存</button>
			</div>
			<div style="height: 50px;"></div>
		</div>

		<!-- js代码 start -->

		<!-- 基础js start -->
		<script src="../../../js/mui.min.js"></script>
		<script src="../../../js/utils/h.js"></script>
		<script src="../../../js/constant.js"></script>
		<script src="../../../js/common.js"></script>
		<script src="../../../js/utils/base64.js"></script>
		<script src="../../../js/utils/md5.js"></script>
		<script src="../../../js/utils/sortMap.js"></script>
		<script src="../../../js/utils/http.js"></script>
		<script src="../../../js/app.js"></script>
		<!-- 基础js end -->
		<script src="../../../js/utils/rolldate.min.js"></script>
		<script src="../../../js/fse/service/serviceLog/log.js"></script>
		
		<script type="text/javascript" charset="utf-8">
			var serviceNo;
			var serviceNoticeId;
			var planId;
			var listName;
			var status;
			var checkOutList;
			var signInTime;
			var formObj = {};
			(function($, doc) {
				$.init();

				$.plusReady(function() {
					var self = plus.webview.currentWebview();
					serviceNoticeId = self.serviceNoticeId;
					serviceNo = self.serviceNo;
					planId = self.planId;
					listName = self.listName;
					checkOutList = self.checkOutList;


					h('#fseId').val(app.getUser().enginnerId)
					h('#serviceNoticeIds').val(self.serviceNoticeId)
					h('#serviceNos').val(self.serviceNo)
					h('#planIds').val(self.planId)
					h('#fseName').val(app.getUser().name)
					h("#serviceNo").html(serviceNo);
					
					// 时间控件初始化
					initDateTimeControls()
					
					// 获取接受任务时间（Send_time）、到达现场时间（on_site_data）
					getServiceTime();
					
					h("#cancelBtn").tap(function() {
						self.close();
					})
					
					// 提交保存
					h('#submitLogBtn').tap(function() {
						h('#submitLogBtn').attr('disabled','disabled');
						if(listName == "signOut"){
							isNoticesLog();
						} else {
							saveFormObj();
						}					
					});

					h("#hdyqxlh").tap(function(){
						ajaxGet(BASE_URL + "cc/notice/getDaiZhiPaiNoticeDetail", {
							"ServiceNoticeId": serviceNoticeId 
						}, function(data) {
							if(data.code==200){
								var serviceType = data.data.serviceType==undefined?"":data.data.serviceType;
								var check = data.data.isCheck==undefined?"":data.data.isCheck;
								if(serviceType=='安装'){
									// if(check=='2'){
									// 	app.toast("已核对，无需重复核对")
									// 	return
									// }else{
										app.jumpTo('/view/service/checkInstrument/checkInstrument.html','view/service/checkInstrument/checkInstrument.html',
										{
											ServiceNoticeId: serviceNoticeId,
											serviceNo: serviceNo,
											planId:planId,	
											state:"log"
										});
									// }
								}else{
									app.toast("非安装单，无需核对")
									return
								}
							}
						}, function(errorMsg) {

						});
					})
					
					h("#glfwtz").tap(function(){
						//判断有没有服务通知列表
						app.showWaiting('加载中...');
						ajaxGet(BASE_URL + "cc/notice/AssociatedCustomers", {
							"ServiceNoticeId": serviceNoticeId,
							"EnginnerId": app.getUser().enginnerId,
							"state": 2,
							// "planId": planId
						}, function(data) {
							app.closeWaiting();
							if(data.code==200){
								if(data.data && data.data.length > 1){  //跳服务通知页
									status == 1;
									app.jumpTo("/view/service/serviceLog/logNotices.html", "view/service/serviceLog/logNotices.html",
									{
										ServiceNoticeId: serviceNoticeId,
										serviceNo: serviceNo,
										planId:planId,
										signInTime:signInTime,
										state: 2
									});
								} else {
									app.toast("无其他服务通知可关联")
								}
							}else{
								 app.toast("获取服务通知列表失败")
							}
						}, function(errorMsg) {
							app.closeWaiting();
							app.toast("获取服务通知列表失败")
						});
					})
				});
			}(mui, document));
			
			// 获取接受任务时间（Send_time）、到达现场时间（on_site_data）
			function getServiceTime() {
				app.showWaiting('加载中...');
				if (serviceNo) {				
					ajaxGet(BASE_URL + "cc/PlanDetails/getDetailsById", {"id":planId}, function(data) {
						app.closeWaiting();
						if (data.code == 200) {//查询成功
							h("#acceptTime").val(data.data.acceptTime);
							signInTime = data.data && data.data.signInTime ? data.data.signInTime : '';
							h("#arriveTime").val(signInTime.substring(0, signInTime.length-3));
						} else {//查询失败
							app.toast("查询失败");
							mui.back();
						}
					}, function(errorMsg) {
						app.closeWaiting();
						app.toast("查询失败");
						mui.back();
					});
				} else {
					app.closeWaiting();
					app.toast("查询失败");
					mui.back();
				}
			}
					
			//判断有没有服务通知列表
			function isNoticesLog(){	
				ajaxGet(BASE_URL + "cc/notice/AssociatedCustomers", {
					"ServiceNoticeId": serviceNoticeId,
					"EnginnerId": app.getUser().enginnerId,
					"state": 2,
					// "planId": planId
				}, function(data) {
					if(data.code==200){
						if(data.data && data.data.length > 1){
							getFWList();
						} else {
							saveFormObj();
						}
					} else {
						h('#submitLogBtn').removeAttr('disabled');
						app.toast(data.msg);
					}
				}, function(errorMsg) {
					app.toast("保存失败");
					h('#submitLogBtn').removeAttr('disabled');
					console.log(errorMsg);
				});
			}
			
			function getFWList(){
				ajaxGet(BASE_URL + "cc/notice/associatedCustomerSrelevance", {
					"ServiceNoticeId": serviceNoticeId,
					"EnginnerId": app.getUser().enginnerId,
					"state": 2,
					"signInTime": signInTime,
					"planId": planId
				}, function(data) {
					if(data.code == 200){
						if(data.data && data.data.length > 0){
							var ServiceNoticeIds = "";
							var serviceNos = "";
							var planIds = "";
							for(var i=0; i < data.data.length; i++){
								planIds += data.data[i].id + ",";
								ServiceNoticeIds += data.data[i].serviceNoticeId + ",";
								serviceNos += data.data[i].serviceNo + ",";
							}
							planIds = planIds.slice(0, planIds.length-1);
							ServiceNoticeIds = ServiceNoticeIds.slice(0, ServiceNoticeIds.length-1);
							serviceNos = serviceNos.slice(0, serviceNos.length-1);
							update(planIds, ServiceNoticeIds, serviceNos);
						} else {
							saveFormObj();
						}
					} else {
						saveFormObj();
					}					
				}, function(errorMsg) {
					saveFormObj();
					console.log(JSON.stringify(errorMsg))
				});
			}
				
			function update(planIds, ServiceNoticeIds, serviceNos) {
				h('#serviceNoticeIds').val(ServiceNoticeIds)
				h('#serviceNos').val(serviceNos)
				h('#planIds').val(planIds)	
				saveFormObj()
			}
			
			function saveFormObj(){
				var subFlag = true;
				mui("#form_info input, #form_info textarea").each(function(index, item) {
					var value = item.value;
					var name = item.getAttribute("name");
					var required = item.getAttribute("required");
					if (required && !value) {
						subFlag = false;
						app.toast(item.getAttribute("placeholder"));
						h('#submitLogBtn').removeAttr('disabled');
						return false;
					}
					formObj[name] = value; //参数赋值
				});
				
				var newDate = new Date().getTime(); // 当前时间
				if (stringYYYYMMDDHHMMToDate(formObj.departureTime).getTime() > newDate) {
					app.toast('出发时间不能大于当前时间')
					h('#submitLogBtn').removeAttr('disabled');
					return false
				}
				
				if (stringYYYYMMDDHHMMToDate(formObj.arriveTime).getTime() > newDate) {
					app.toast('到达现场时间不能大于当前时间')
					h('#submitLogBtn').removeAttr('disabled');
					return false
				}
				
				if (stringYYYYMMDDHHMMToDate(formObj.startTime).getTime() > newDate) {
					app.toast('工作开始时间不能大于当前时间')
					h('#submitLogBtn').removeAttr('disabled');
					return false
				}
				
				if (stringYYYYMMDDHHMMToDate(formObj.endTime).getTime() > newDate) {
					app.toast('工作结束时间不能大于当前时间')
					h('#submitLogBtn').removeAttr('disabled');
					return false
				}
				
				if (subFlag) {
					// 时间关系校验
					var checkFlag = checkTimes();
					if (!checkFlag) {
						h('#submitLogBtn').removeAttr('disabled');
						return false;
					}
					// 非负数校验
					var journeyHour = h("#journeyHour").val();
					var workHour = h("#workHour").val();
					var patt = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/
					if (patt.test(workHour) == false) {
						app.toast("请输入有效工作小时数");
						h('#submitLogBtn').removeAttr('disabled');
						return false;
					}
					if (patt.test(journeyHour) == false) {
						app.toast("请输入有效旅途小时数");
						h('#submitLogBtn').removeAttr('disabled');
						return false;
					}
					
					if(Number(workHour)>Number(gztimes)){
						app.toast("工作小时数，不能大于（工作结束时间-工作开始时间）");
						h('#submitLogBtn').removeAttr('disabled');
						return false;
					}
					
					// 20200713 要求去掉这个判断
					/* if(Number(journeyHour)>Number(lttimes)){
						app.toast("旅途小时数，不能大于（到达现场时间-出发时间）");
						h('#submitLogBtn').removeAttr('disabled');
						return false;
					} */
					
					// 批量需求调整 不在询问是否拆分工时，所有关联的都拆分 20200704
					if (formObj.serviceNos.split(",").length > 1){
						// mui("#popover").popover('toggle', document.getElementById("div"));
						saveLog('1');
					} else {
						saveLog('0');
					}
				}
			}
			
			// 保存日志
			function saveLog(splitFlag) {
				// mui("#popover").popover('hide', document.getElementById("div"));
				formObj.splitFlag = splitFlag ? splitFlag : "0";
				var self = plus.webview.currentWebview();
				app.showWaiting('加载中...');
				ajaxPost(BASE_URL + "cc/ServiceLog/saveServiceLog", formObj, function(data) {
					app.closeWaiting()
					h('#submitLogBtn').removeAttr('disabled');
					if (data.code == 200) {//查询成功
						if(splitFlag == "1"){
							app.toast("保存成功,工时已平均拆分到已关联的多个服务通知");
						} else {
							app.toast("保存成功");
						}					
						if(listName == "checkedOutList"){  //跳转到签离再次上门   /Users/solutiona/Desktop/project/FSE-H5/view/service/notice/signOutVisitAgain.html
							/*app.jumpTo("/view/service/notice/signOutVisitAgain.html", "view/service/notice/signOutVisitAgain.html",{
									ServiceNoticeId: serviceNoticeId,
									serviceNo: serviceNo,
									planId:planId,
									fseName:app.getUser().name
							});*/
							var signOutVisitAgain = plus.webview.create('/view/service/notice/signOutVisitAgain.html','view/service/notice/signOutVisitAgain.html',null,
									{
										ServiceNoticeId: serviceNoticeId,
										serviceNo: serviceNo,
										planId:planId,
										fseName:app.getUser().name
									})
							signOutVisitAgain.show()
							self.close();
						}
						if(listName == "signOut"){ 	//跳转到签离添加报告
							var signOut = plus.webview.getWebviewById('view/service/notice/signOut.html');
							if (signOut) {
								signOut.close()
							}
				
							app.jumpTo("/view/service/notice/signOutAddReport.html", "view/service/notice/signOutAddReport.html",{
									ServiceNoticeId: serviceNoticeId,
									serviceNo: serviceNo,
									planId:planId,
									fseName:app.getUser().name
							});
						}
						if(listName == "signInAddReport"){ 	//返回上一页
							// 查找需要跳转的页面，并指定刷新的方法
							var assignList = plus.webview.getWebviewById('view/service/notice/signInAddReport.html');
							assignList.evalJS('judgeLog()')
							mui.back()
						}
						if(listName == "indexFSE"){ 	//返回上一页
							// 查找需要跳转的页面，并指定刷新的方法
							var indexFSE = plus.webview.getWebviewById('view/main/indexFSE.html');
							indexFSE.evalJS('getList("1")')
							mui.back()
						}
						if(listName == "yiqianli"){ 	//返回上一页
							var signInAddReport = plus.webview.create('/view/service/notice/signInAddReport.html','view/service/notice/signInAddReport.html',null,
							{
								ServiceNoticeId: serviceNoticeId,
								serviceNo: serviceNo,
								planId:planId,
							})
							signInAddReport.show()
							self.close();
						}
						if(listName == "autoSignOut"){
							// var self = plus.webview.currentWebview();
							// self.close();
							// var signInAddReport = plus.webview.create('/view/service/notice/signOutVisitAgain.html','view/service/notice/signOutVisitAgain.html',null,
							// {
							// 	ServiceNoticeId: serviceNoticeId,
							// 	serviceNo: serviceNo,
							// 	planId:planId,
							// })
							// signInAddReport.show()
							// self.close();
							
							// 跳出花了，让跳啥进啥吧，已完成列表
							var completedList = plus.webview.getWebviewById('view/service/notice/completedList.html');
							if(completedList){
								completedList.evalJS('initData()')
								mui.back()
							}else{
								var completedList1 = plus.webview.create('/view/service/notice/completedList.html','view/service/notice/completedList.html')
								completedList1.show()
								self.close();
							} 
						}
						if(listName == "logFill"){ 	//返回上一页
							// 查找需要跳转的页面，并指定刷新的方法
							var logFill = plus.webview.getWebviewById('logFill');
							logFill.evalJS('selectList()');
							mui.back();
						}
						if (checkOutList){
							var autoCheckedOutList = plus.webview.create('/view/service/notice/checkedOutList.html','view/service/notice/checkedOutList.html');
							autoCheckedOutList.show();
							self.close();
						}
					} else {
						app.toast(data.msg);
					}
				}, function(errorMsg) {
					app.closeWaiting()
					app.toast("保存失败");
					h('#submitLogBtn').removeAttr('disabled');
				});
			}
			h("#dianji").tap(function(){
				app.jumpTo("/view/workArea/service/serveDetails.html", "view/workArea/service/serveDetails.html", {
					"serviceNo": serviceNo,
					"enginnerId": app.getUser().enginnerId
				})
			})
		</script>
		
		<!-- js代码 end -->
	</body>

</html>

